<?php

// prevent mishaps while reading config
$PARA_PRE_SETUP = 1;

try {
    require "config/config.php";
    require "classes/init.php";
} 
catch (Exception $e) {
    echo "Some exception $e";
    exit;
}

require "include/tables.php";
require "include/defaultvalues.php";


$action = $_GET['action'];

$dbl = $PARAPARA->dblayer;

$tables_ok = $dbl->check_tables();

$action_done = false;
if( $tables_ok && ($action == 'droptables') ) {
    $action_status = $dbl->drop_tables( true );
    $tables_ok = $dbl->check_tables();
    $action_done = true;
}
elseif(!$tables_ok && ($action == 'createtables')) {
    $action_status =  $dbl->create_tables( true );
    $dbl->insert_defaults();
    $tables_ok = $dbl->check_tables();
    $action_done = true;
}



?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
   <title>Paraforum Setup</title>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
   <link rel="stylesheet" type="text/css" media="screen" href="styles/setup.css" />

</head>
<body>

<h1>Welcome!</h1>
<p> This is the Paraforum setup script. </p>

<?
    if($action_done) {
        echo "<p>action:$action <br/>\n";
        if($action_status) {
            echo "status: complete success</p>\n";
        }
        else {
            echo "status: partial success or partial failure</p>\n";
        }
    }
?>

<hr />

<?
if($tables_ok) {
?>
<p>The tables are already set up.</p>
<p><a href="?action=droptables">Drop tables</a></p>

<? }
else {
?>
<p>The tables are not set up yet.</p>
<p><a href="?action=createtables">Create tables</a></p>
<?}
?>

</body>
</html>
